//
//  NBStatusOriginView.swift
//  NBSinaWeibo
//
//  Created by ac on 16/5/24.
//  Copyright © 2016年 ac. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class NBStatusOriginView: UIView {
    
    private var bottonCons: Constraint?
    
    var statusVM: NBStatusViewModel?{
        didSet{
            
//            let headURL = NSURL(string: statusVM?.status?.user?.profile_image_url ?? "")
            
            headIcon.sd_setImageWithURL(statusVM?.headURL)
            nameLabel.text = statusVM?.status?.user?.name
            verifiedImageView.image = statusVM?.verifiedImage
//            verifiedImageView.hidden = statusVM?.status?.user?.verified == 0
//            sourceLabel.text = statusVM?.status?.source
            sourceLabel.text = statusVM?.source
//            timeLabel.text = statusVM?.status?.created_at
            timeLabel.text = statusVM?.time
            contentLabel.text = statusVM?.status?.text
            mbrankImageView.image = statusVM?.mbrankImage
//            if statusVM?.status?.user?.mbrank > 0 && statusVM?.status?.user?.mbrank < 7 {
//                // 1 ~ 6
//                //转译会自动加上 optional
//                let image = UIImage(named: "common_icon_membership_level\(statusVM?.status?.user?.mbrank ?? 0)")
//                mbrankImageView.image = image
//            }
            
//            pictuerView.pictures = statusVM?.status?.pic_urls
            
            self.bottonCons?.uninstall()
            if let picurls = statusVM?.status?.pic_urls where picurls.count > 0{
                pictuerView.pictures = picurls
                self.snp_updateConstraints(closure: { (make) in
                    self.bottonCons = make.bottom.equalTo(pictuerView.snp_bottom).offset(CellMargin).constraint
                })
                pictuerView.hidden = false
            }else{
                pictuerView.pictures = nil
                self.snp_updateConstraints(closure: { (make) in
                    self.bottonCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(CellMargin).constraint
                })
                pictuerView.hidden = true
            }
        }
    }
    
    
    private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    private lazy var nameLabel = UILabel(title: "脑残片推销员", fontSize: 14.0, textColor: UIColor.darkGrayColor())
    private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    private lazy var sourceLabel = UILabel(title: "呵呵的iphone", fontSize: 10, textColor: UIColor.darkGrayColor())
    private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    private lazy var contentLabel = UILabel(title: "呵呵的iphone呵呵的iphone呵呵的iphone呵呵的iphone呵呵的iphone呵呵的iphone呵呵的iphone", fontSize: 14, textAlign: NSTextAlignment.Left, textColor: UIColor.darkGrayColor())
    private lazy var timeLabel = UILabel(title: "11:11", fontSize: 10, textColor: UIColor.orangeColor())
    private lazy var sepView = UIView()
    
    
    private lazy var pictuerView:NBStatusPictureView = NBStatusPictureView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        updateUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func updateUI(){
//        backgroundColor = UIColor.greenColor()
        addSubview(headIcon)
        addSubview(nameLabel)
        addSubview(verifiedImageView)
        addSubview(sourceLabel)
        addSubview(mbrankImageView)
        addSubview(contentLabel)
        addSubview(timeLabel)
        
        addSubview(pictuerView)
        addSubview(sepView)
        sepView.backgroundColor = UIColor.lightGrayColor()
        
        sepView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self)
            make.height.equalTo(2 * CellMargin)
        }
        
        headIcon.snp_makeConstraints { (make) in
            make.top.equalTo(sepView.snp_bottom).offset(CellMargin)
            make.left.equalTo(self).offset(CellMargin)
            make.height.equalTo(40)
            make.width.equalTo(40)
        }
        
        verifiedImageView.snp_makeConstraints { (make) in
            make.right.bottom.equalTo(headIcon)
        }
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(headIcon.snp_right).offset(CellMargin)
        }
        
        mbrankImageView.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_top)
            make.left.equalTo(nameLabel.snp_right).offset(CellMargin)
        }
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(headIcon.snp_right).offset(CellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        sourceLabel.snp_makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp_right).offset(CellMargin)
            make.bottom.equalTo(headIcon.snp_bottom)
        }
        
        contentLabel.snp_makeConstraints { (make) in
            make.top.equalTo(headIcon.snp_bottom).offset(CellMargin)
            make.left.equalTo(headIcon.snp_left)
        }
        
        pictuerView.snp_makeConstraints { (make) in
            make.top.equalTo(contentLabel.snp_bottom).offset(CellMargin)
            make.left.equalTo(self)
            
            //TUDO: 是指测试大小
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        self.snp_makeConstraints { (make) in
            self.bottonCons = make.bottom.equalTo(pictuerView.snp_bottom).offset(CellMargin).constraint
        }
        
        
        headIcon.cornerRadius = 20
        
    }
}
